How to redirect to another URL?
# help-with-umbraco
t
Hi I thought this might have been easy but struggling to redirect to another page in Umbraco which I know exists. I have a View which is decorated as below using(Html.BeginUmbracoForm(".....") { Controls in here } I then have my controller inheriting from SurfaceController. At the end of this void method I have added some code to redirect to another Umbraco page. Currently I have RedirectToPage("/site/thanks"); When I click the button on the View, the page disappears but the URL remains the same as the current page. What could I be missing? Thanks
s
Return missing? E.g/
Copy code
return RedirectToPage("/site/thanks");
AH - RedirectToPage also takes a "page name" - I think you want just Redirect()
t
Hi it's a void method. I just tried the old way Response.redirect("/page....."); Which seems to have done the trick but not sure if it's the recommended way?
s
Wait - your method that your form posts to in the surface controller is void? https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers It should be IActionResult (v10-13+ ... you don't say what version of Umbraco you have)
t
Yes it's void as it carries out since operations and then I like to redirect from the method.... Is that bad practice? Just wondering now?
s
I'm not 100% here but if you need to redirect then you need the form post to have an action returned. Not sure I'm following why you've made it void - the return Redirect will do what you need but obviously it needs to return the redirect in an IAction
How are you redirecting from the view? I suppose you could set a tempdata / view data param in the void method and look for it in the view and then redirect... but seems a bit messy to me.
t
I'm not redirecting from view. All I do is set some information on this view. Press the submit button. This calls the void method. Upon completion of this method I just redirect the user to a thanks page. I suppose I could breakdown the method and split the task to complete and once completed return it in an IActionResult. I was using response.redirect which is working now but I wasn't sure if this was the correct Umbraco way of doing this
s
Sure - if you look at the documentation I posted the link to the method to post to is not a void but returns an Action. I think this is required here to do it the proper way. But if it's working and you're happy? I've seen Safari do weird things with surface controller posts so ensure you test it on that.
383 Views